package com.example.rubbish.Fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import com.example.rubbish.R;
import com.example.rubbish.Ui.HelpDialogView;
import com.example.rubbish.Utils.ShareHelper;
import com.google.android.material.switchmaterial.SwitchMaterial;


public class SettingFragment extends Fragment {
    private  View root;
   private ShareHelper shareHelper;
   private SwitchMaterial aSwitch;
    private boolean isCheck;
    TextView button_help;


    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

       root = inflater.inflate(R.layout.fragment_setting, container,false);
        shareHelper= new ShareHelper(root.getContext());
        button_help= root.findViewById(R.id.button_help);
        aSwitch = root.findViewById(R.id.switch_auto);

        isCheck=shareHelper.getSettingSpeak();
        aSwitch.setChecked(isCheck);


       return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        button_help.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                HelpDialogView helpDialogView = new HelpDialogView(root.getContext());
                TextView textView = helpDialogView.findViewById(R.id.scroll_text);
                setText(textView);
                AlertDialog alertDialog = new AlertDialog.Builder(root.getContext())
                        .setTitle("说明")
                        .setView(helpDialogView)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        })
                        .create();
                alertDialog.show();
                WindowManager.LayoutParams params = alertDialog.getWindow().getAttributes();
                params.width = WindowManager.LayoutParams.WRAP_CONTENT;
                params.height = WindowManager.LayoutParams.WRAP_CONTENT ;
                alertDialog.getWindow().setAttributes(params);

            }
        });

        aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(compoundButton.isChecked()){
                    shareHelper.save(true);
                    Toast.makeText(root.getContext(),"已打开自动语言播报",Toast.LENGTH_SHORT).show();
                }else {
                    shareHelper.save(false);
                    Toast.makeText(root.getContext(),"已关闭自动语言播报",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }




    private void setText(TextView textView) {
        textView.setMovementMethod(ScrollingMovementMethod.getInstance());
        textView.setText("该应用为一款资源分类回收辅助APP，借此来实现快速的资源收集，" +
                "提高资源合成计划的材料收集阶段的速度，" +
                "功能包括提供垃圾分类，物品回收建议，语音播报，图片识别等功能。" +
                "应用目前处于初期开发，"+
                "其他功能仍在开发，尽情期待\n");
    }
}